package com.finalcial.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.finalcial.service.PowerService;

@Controller
@RequestMapping("/power")
public class PowerController {
	@Autowired
	private PowerService powerService;
	
	@RequestMapping("/listUserSketchyMsg")
	public String listUserSketchyMsg(HttpServletRequest request, HttpServletResponse response) {
		List<Map<String, Object>> userSketchyMsgList = powerService.listUserSketchyMsg();
		List<Map<String, Object>> userRoleNameList = powerService.listRoleName();
		for(Map<String, Object> map:userSketchyMsgList) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		System.out.println("-------------");
		for(Map<String, Object> map:userRoleNameList) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		request.setAttribute("userRoleNameList",userRoleNameList);
		request.setAttribute("userSketchyMsgList",userSketchyMsgList);
		return "systemsetting/power";
	}
	
	@RequestMapping("/getUserRoleList")
	public String getUserRoleList(HttpServletRequest request, HttpServletResponse response) {
		List<Map<String, Object>> userRoleNameList = powerService.listRoleName();
		for(Map<String, Object> map:userRoleNameList) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		request.setAttribute("userRoleNameList",userRoleNameList);
		return "systemsetting/userRole";
	}
	
	/**
	 * 通过父ID查询子节点
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getUserRoles")
	public HashMap<String, Object> getUserRoles(HttpServletRequest request, HttpServletResponse response) {
		String parentId = request.getParameter("parentId");
		String roleId = request.getParameter("roleId");
		HashMap<String, Object> map = new HashMap<String, Object>();
		if("".equals(parentId) == false || parentId != "") {
			String ssJson = powerService.getUserRoles(Integer.parseInt(parentId), "[", roleId);
			System.out.println(ssJson);
			map.put("ssJson", ssJson);
			return map;
		}
		return null;
	}
}
